package co.fitstart.admin.entity.search;

import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.round.RoundStatus;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

public class CourseRoundSearch implements QueryBuilder {
    private Site site;
    
    private List<Site> sites;
    
    private String name;
    
    private String mobile;
    
    private Principal coach;
    
    private RoundStatus roundStatus;
    
    private CourseType courseType;
    
    private Order order;
    
    private Filter filter;
    
    public CourseRoundSearch() {
        order = new Order("createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        if(filter == null || refresh) {
            
            filter =  new Filter().addSentence("site IN (:sites)", "sites", sites)
                                  .addEquals("disabled", false)
                                  .addEquals("courseStatus", roundStatus)
                                  .addEquals("site", site)
                                  .addEquals("coach", coach)
                                  .addLike("student.name", name)
                                  .addLike("student.mobile", mobile);
            
            if(courseType != null && (courseType.isIndividual()  || courseType.isSpecial() || courseType.isPosture() || courseType.isYear())) {
                filter.addEquals("courseType", courseType);
            } else {
                CourseType[] courseTypes = new CourseType[]{CourseType.INDIVIDUAL, CourseType.SPECIAL, CourseType.POSTURE, CourseType.YEAR};
                
                filter.addSentence("courseType in( :courseTypes )", "courseTypes", courseTypes);
            }
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM CourseRound " + getPreparedFilter(false).getWhereSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the site
     */
    public Site getSite() {
        return site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the coach
     */
    public Principal getCoach() {
        return coach;
    }

    /**
     * @return the roundStatus
     */
    public RoundStatus getRoundStatus() {
        return roundStatus;
    }

    /**
     * @return the courseType
     */
    public CourseType getCourseType() {
        return courseType;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Site site) {
        this.site = site;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param coach the coach to set
     */
    public void setCoach(Principal coach) {
        this.coach = coach;
    }

    /**
     * @param roundStatus the roundStatus to set
     */
    public void setRoundStatus(RoundStatus roundStatus) {
        this.roundStatus = roundStatus;
    }

    /**
     * @param courseType the courseType to set
     */
    public void setCourseType(CourseType courseType) {
        this.courseType = courseType;
    }
}
